//
//  CPYAFNetTool.m
//  Summer
//
//  Created by chenpengyu on 16/3/7.
//  Copyright © 2016年 cpy. All rights reserved.
//

#import "CPYAFNetTool.h"
#import "AFNetworking.h"
@implementation CPYAFNetTool

+ (void)GETNetWithUrl:(NSString *)url body:(id)body headerFile:(NSDictionary *)header response:(CPYResponseStyle)responseStyle success:(void (^)(id result))success failure:(void (^)(NSError * error))failure {
    
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    
    NSString *cahcePath = [NSString stringWithFormat:@"%@/%@.plist", path, [url md5ForString]];
    
    // 1. 设置网络管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 2. 设置请求头
    if (header) {
        for (NSString *key in header.allKeys) {
            [manager.requestSerializer setValue:header[key] forHTTPHeaderField:key];
        }
    }
    // 3. 设置返回数据的类型
    switch (responseStyle) {
        case CPYJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case CPYXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case CPYDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        default:
            break;
    }
    // 4. 设置响应数据的类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    // 5. UTF-8转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    // 6. 请求数据
    [manager GET:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            success(responseObject);
            
                //                成功归档
                [NSKeyedArchiver archiveRootObject:responseObject toFile:cahcePath];
            
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            failure(error);
            if (error) {
                
                id responseObject = [NSKeyedUnarchiver unarchiveObjectWithFile:cahcePath];
                
                if (responseObject) {
                    success(responseObject);
                }
                failure(error);
                
            }
        }
    }];
}
+ (void)POSTNetWithUrl:(NSString *)url body:(id)body bodyStyle:(CPYRequestStyle)bodyStyle headerFile:(NSDictionary *)header response:(CPYResponseStyle)responseStyle success:(void (^)(id result))success failure:(void (^)(NSError * error))failure {
    // 1. 设置网络管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 2. 设置body体数据类型
    switch (bodyStyle) {
        case CPYrequestJSON:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        case CPYRequestString:
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
                return parameters;
            }];
            break;
        default:
            break;
    }
    // 3. 设置请求头
    if (header) {
        for (NSString *key in header.allKeys) {
            [manager.requestSerializer setValue:header[key] forHTTPHeaderField:key];
        }
    }
    // 4. 设置返回数据的类型
    switch (responseStyle) {
        case CPYJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case CPYXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case CPYDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        default:
            break;
    }
    // 5. 设置响应数据的类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    // 6. UTF-8转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    // 7. 请求数据
    [manager POST:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            failure(error);
        }
    }];
}
#pragma mark - 本地缓存
- (void)localSave:(NSString *)mainUrl {
    NSArray *path1 = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *patht12 = [path1 lastObject];
//    NSString *judgement = [mainUrl substringWithRange:NSMakeRange(<#NSUInteger loc#>, <#NSUInteger len#>)]
    patht12 = [patht12 stringByAppendingString:@"/mainModel.plist"];
    [CPYAFNetTool GETNetWithUrl:mainUrl body:nil headerFile:nil response:CPYJSON success:^(id result) {
        
    } failure:^(NSError *error) {
        
    }];
    
}


@end
